PLUGIN_NAME = "Ami (Metadata)"
PLUGIN_AUTHOR = "SuperSaltyGamer"
PLUGIN_DESCRIPTION = "Enhancements to the metadata pane."
PLUGIN_VERSION = "1.0.0"
PLUGIN_API_VERSIONS = ["2.10"]
PLUGIN_LICENSE = "AGPL-3.0"

from picard.config import config
from picard.ui.metadatabox import MetadataBox, TagDiff
from picard.util.tags import TAG_NAMES

TAG_NAMES.update({
	"comment": "Comment",
	"disctotal": "Disc Total",
	"encoder": "Encoder",
	"mqa": "MQA",
	"source": "Source",
	"tracktotal": "Track Total",
	"upc": "UPC",
	"url": "URL",
})

original_update_tags = MetadataBox._update_tags


def override_update_tags(self: MetadataBox, new_selection=True, drop_album_caches=False):
	ret: TagDiff = original_update_tags(self, new_selection, drop_album_caches)
	if ret is None:
		return ret
	last_index = -1
	for top_tag in config.setting["metadatabox_top_tags"]:
		forced = top_tag.startswith("!")
		top_tag = top_tag.lstrip("!")
		index = next((i for i, tag in enumerate(ret.tag_names) if tag == top_tag), -1)
		if index != -1:
			last_index = index
		elif forced:
			last_index += 1
			ret.tag_names.insert(last_index, top_tag)
	return ret


MetadataBox._update_tags = override_update_tags
